package com.testmall.controller;

import com.testmall.entity.Product;
import com.testmall.service.CartService;
import com.testmall.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Description:
 * @Author: littleDesire
 * @Date: 2022-06-24 21:50
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/cart")
@Api(value = "购物车", tags = "购物车接口")
public class CartController {
    @Autowired
    ProductService productService;
    @Autowired
    CartService cartService;

    @PostMapping("/query")
    @ApiOperation(value = "查询购物车", notes = "查询购物车")
    public Map<String, Object> queryCart(HttpSession session) {
        // cart 里边存放了商品的 proid 以及对应的数量
        Map<String, Object> result = (Map<String, Object>) session.getAttribute("cart");
        Map<String, Object> map = new HashMap<>();
        if (null == result) {
            map.put("msg", "购物车空空如也，赶快去购物吧！！！");
            return map;
        }
        Set<Map.Entry<String, Object>> entries = result.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        String[] proId = new String[entries.size()];
        int i = 0;
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            proId[i] = next.getKey();
            i++;
        }
        List<Product> list = new ArrayList<>();
        List<Product> products = productService.queryCartProduct(proId);
            for (Product product : products) {
                list.add(product);
                result.put("data", list);
            }
        return result;
    }

    @ApiOperation(value = "向购物车添加商品", notes = "添加商品表的商品到购物车")
    @PostMapping("/save")
    public String saveCart(String proId, HttpServletRequest request) {
        // 从session中获取购物车
        Map<String, Integer> mapCart = (Map<String, Integer>) request.getSession().getAttribute("cart");
        // 判断购物车是否为空
        Integer count = null;
        if (mapCart == null) {
            // 第一次购物
            mapCart = new HashMap<>();
            // 将购物车放入到session中
            request.getSession().setAttribute("cart", mapCart);
            count = 1;
        } else {
            // 存在购物车
            // 然后判断购物车是否有该商品
            // 获取商品数量
            count = mapCart.get(proId);
            if (count == null) {
                // 没有该商品
                count = 1;
            } else {
                count++;
            }
        }
        // 将商品放入购物车
        mapCart.put(proId, count);
        Product product = productService.getById(Integer.parseInt(proId));
        return String.format("商品：%s ，已添加到购物车！！", product.getName());
    }
}
